#include <stdio.h>
#include <algorithm>
using namespace std;

int n;
int shdist[310][310];
int k;
int a, b, v;
long long sum;

int main() {
	scanf("%d", &n);

	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++)
			scanf("%d", &shdist[i][j]);
	}

	scanf("%d", &k);

	while (k--) {
		scanf("%d %d %d", &a, &b, &v);
		a--; b--;

		if (shdist[a][b]>v) {
			shdist[a][b]=v;

			for (int i=0; i<n; i++) {
				for (int j=0; j<n; j++) {
					shdist[i][j] = min(shdist[i][j], shdist[i][a]+v+shdist[b][j]);
					shdist[i][j] = min(shdist[i][j], shdist[j][a]+v+shdist[b][i]);
				}
			}
		}

		sum=0;

		for (int i=0; i<n; i++) 
			for (int j=0; j<n; j++) sum+= shdist[i][j];

		printf("%I64d ", sum/2);
	}

	printf("\n");

	return 0;
}
